package io.geekidea.boot.util.StringUtils;

import org.springframework.stereotype.Component;

@Component
public class UrlToJson {

    public static String generateFileInfoJson(String downloadUrl, String name, String previewUrl, String ext) {
        // 使用字符串拼接构建JSON
        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.append("{")
                .append("\"downloadUrl\": \"").append(escapeJson(downloadUrl)).append("\",")
                .append("\"name\": \"").append(escapeJson(name)).append("\",")
                .append("\"previewUrl\": \"").append(escapeJson(previewUrl)).append("\",")
                .append("\"url\": \"").append(escapeJson(downloadUrl)).append("\",")
                .append("\"ext\": \"").append(escapeJson(ext)).append("\"")
                .append("}");

        return jsonBuilder.toString();
    }

    /**
     * 简单的JSON转义处理
     * @param value 需要转义的字符串
     * @return 转义后的字符串
     */
    private static String escapeJson(String value) {
        if (value == null) {
            return "";
        }
        return value.replace("\\", "\\\\")
                .replace("\"", "\\\"")
                .replace("\b", "\\b")
                .replace("\f", "\\f")
                .replace("\n", "\\n")
                .replace("\r", "\\r")
                .replace("\t", "\\t");
    }
}
